package com.example.accessingdatajpa.controller;

import com.example.accessingdatajpa.pojo.Customer;
import com.example.accessingdatajpa.pojo.Role;
import com.example.accessingdatajpa.response.RestResponse;
import com.example.accessingdatajpa.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/app")
public class CustomerController {

    @Autowired
    final private ICustomerService customerService;

    public CustomerController(ICustomerService customerService) {
        this.customerService = customerService;
    }

    //find all customers
    @GetMapping("/getAllCustomer")
    public @ResponseBody RestResponse<List<Customer>> queryAllCustomers(){
        return customerService.queryAllCustomers();
    }

    //add a new customer
    @PostMapping("/addCustomer")
    public @ResponseBody RestResponse<String> addACustomer(@RequestBody Customer customer, HttpServletRequest request){
        return customerService.addOneCustomer(customer,request);
    }

    //add a new customer (the part with role)
    @PostMapping("/updateAuthority")
    public @ResponseBody RestResponse<Role> addOneCustomer(@RequestParam Long customerId, @RequestParam Long roleId){
        return customerService.updateCustomerAuthority(customerId,roleId);
    }

    //login
    @PostMapping("/login")
    public @ResponseBody RestResponse<Long> customerLogin(@RequestParam String userName, @RequestParam String userPassword){
        return customerService.authenticateCustomer(userName,userPassword);
    }

    //delete a customer
    @DeleteMapping("/delete/{customerId}")
    public @ResponseBody RestResponse<String> deleteCustomer(@PathVariable("customerId") Long customerId){
        return customerService.deleteOneCustomer(customerId);
    }
}
